package carroejb;

import java.sql.Timestamp;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entidades.Carro;



/**
 * Session Bean implementation class CarroEJB
 */
@Stateless (name = "CEJB", mappedName = "CEJB")
public class CarroEJB implements CarroEJBRemote {

    @PersistenceContext (unitName = "JPATINET")
    private EntityManager EM;
    
    
    /**
     * Default constructor. 
     */
    public CarroEJB() {
        
    }

    /* (non-Javadoc)
     * @see carroejb.CarroEJBRemote#AddCarro()
     */
    public void AddCarro() {
        List<Carro> lista = GetListCarro();
        int codfinal;
        Carro c = new Carro();
        if (lista.isEmpty()) {
            codfinal = 1;
            c.setIdCarro(codfinal);
        } else {
            codfinal = lista.size() + 1;
            c.setIdCarro(codfinal);
        }
        EM.persist(c);
        
    }

    /* (non-Javadoc)
     * @see carroejb.CarroEJBRemote#AddLogin()
     */
    public void AddLogin(int idCarro, String username) {
        Carro c = EM.find(Carro.class, idCarro);
        c.setUsuario(username);
        
    }

    /* (non-Javadoc)
     * @see carroejb.CarroEJBRemote#RealizarCompra()
     */
    public void RealizarCompra(int idCarro, Timestamp ts) {
        Carro c = EM.find(Carro.class, idCarro);
        c.setFecha_compra(ts);
    }

    /* (non-Javadoc)
     * @see carroejb.CarroEJBRemote#GetListCarro()
     */
    public List<Carro> GetListCarro() {
        Query q = EM.createQuery("Select c from Carro c");
        @SuppressWarnings("unchecked")
        List<Carro> listC = (List<Carro>)q.getResultList();
        return listC;
    }

}
